package com.speed.config;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.config.Ini;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.util.Factory;

import com.jfinal.kit.StrKit;

public enum IniSecurrityManager {

	INSTANCE;

	// 前缀
	private static final String PREFIX = "classpath:";

	private Factory<SecurityManager> factory = null;

	public void setDefaultPath() {
		factory = new IniSecurityManagerFactory();
		init();
	}

	public void setOtherIniConfigByObject(Ini ini) {
		if (ini != null) {
			factory = new IniSecurityManagerFactory(ini);
			init();
		}
	}

	public void setOtherIniConfigByFilePath(String filePath) {
		if (StrKit.notBlank(filePath)) {
			factory = new IniSecurityManagerFactory(PREFIX + filePath);
			init();
		}
	}

	private void init() {
		SecurityManager securityManager = factory.getInstance();
		SecurityUtils.setSecurityManager(securityManager);
	}
}